import datetime

from fastapi import Request
from sqlalchemy.orm import Session

from core.dao.cms_terminal_point_dao import CmsTerminalPointDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsTerminalPointVO


class CmsTerminalPointService:
    """
    高德终端service
    """

    @classmethod
    def get_cms_terminal_point_by_id(cls, result_db: Session, id: int):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param id: id
        :return: 参数配置列表信息对象
        """
        cms_terminal_point = CmsTerminalPointDao.get_cms_terminal_point_by_id(result_db, id)

        return cms_terminal_point

    @classmethod
    def get_cms_terminal_point_by_terminal_id(cls, result_db: Session, terminal_id: str, start_time:str = None, end_time: str = None):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param terminal_id: 高德终端id
        :return: 参数配置列表信息对象
        """

        cms_terminal_point_list = CmsTerminalPointDao.get_cms_terminal_point_by_terminal_id(result_db, terminal_id,datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S'),datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S'))

        return cms_terminal_point_list

    @classmethod
    async def add_cms_terminal_point(cls, request: Request, result_db: Session, cms_terminal_point: CmsTerminalPointVO):
        """
        新增参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_terminal_point: 新增参数配置对象
        :return: 新增参数配置校验结果
        """
        try:
            CmsTerminalPointDao.add_cms_terminal_point_dao(result_db, cms_terminal_point)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    async def edit_cms_terminal_point(cls, request: Request, result_db: Session, cms_terminal_point: CmsTerminalPointVO):
        """
        编辑参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_terminal_point: 编辑参数配置对象
        :return: 编辑参数配置校验结果
        """
        try:
            edit_post = cms_terminal_point.dict(exclude_unset=True)
            CmsTerminalPointDao.edit_cms_terminal_point_dao(result_db, edit_post)
            result_db.commit()
            result = dict(is_success=True, message='修改成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    def delete_cms_terminal_point(cls, result_db: Session, id: int):
        """
        删除菜单信息service
        :param result_db: orm对象
        :param id: id
        :return: 删除终端
        """
        if id > 0:
            try:
                CmsTerminalPointDao.delete_cms_terminal_point_dao(result_db, id)
                result_db.commit()
                result = dict(is_success=True, message='删除成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message='传入高德终端id为空')
        return CrudResponse(**result)

    @classmethod
    def get_cms_terminal_point_by_order_id(cls, query_db, order_id):
        """
           获取参数配置列表信息service
           :param result_db: orm对象
           :param terminal_id: 高德终端id
           :return: 参数配置列表信息对象
           """
        cms_terminal_point_list = CmsTerminalPointDao.get_cms_terminal_point_by_order_id(query_db, order_id)

        return cms_terminal_point_list

